package com.lin.videospringboot.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lin.videospringboot.been.Video;
import com.lin.videospringboot.been.VideoComment;
import com.lin.videospringboot.mapper.VideoCommentMapper;
import com.lin.videospringboot.mapper.VideoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VideoServiceImpl {

    @Autowired
    VideoMapper videoMapper;


    @Autowired
    VideoCommentMapper videoCommentMapper;


    public List<Video> queryAll() {
        return videoMapper.selectList(null);
    }


    public Video getVideoById(int videoId) {
       return videoMapper.selectById(videoId);
    }

    public int updateVideo(Video video) {
        return videoMapper.updateById(video);
    }

    public int addVideo(Video video) {
        return videoMapper.insert(video);
    }

    public List<Video> getListWithURL() {
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("url");
        return videoMapper.selectList(queryWrapper);
    }

    public List<Video> getVideoListByUserId(int userId) {
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        return videoMapper.selectList(queryWrapper);
    }

    public List<Video> searchVideo(String name) {
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("videoName",name);
        queryWrapper.isNotNull("url");
        return videoMapper.selectList(queryWrapper);
    }


    public List<VideoComment> getVideoComments(int videoId) {
        QueryWrapper<VideoComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("videoId",videoId);
        return videoCommentMapper.selectList(queryWrapper);
    }

    public int updateVideoURL(String url,int videoId) {
        Video video = videoMapper.selectById(videoId);
        video.setUrl(url);
        return videoMapper.updateById(video);
    }

}
